package vn.edu.hut.soict.translate.ui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;

import vn.edu.hut.soict.translate.Translator;
import vn.edu.hut.soict.translate.wordgraph.WordUtils;

public class TranslatorUI extends JFrame implements ActionListener {
	private static final long serialVersionUID = 7362611924878884231L;

	private JPanel contentPane;
	private Translator translator;
	private JButton buttonTranslate;
	private JTextArea textAreaInput;
	private JTextArea textAreaOuput;
	private final ButtonGroup buttonGroupMode = new ButtonGroup();
	private JTextArea textAreaViSentence;
	private JTextArea textAreaEnSentence;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TranslatorUI frame = new TranslatorUI();
					frame.setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TranslatorUI() {
		setTitle("D\u1ECBch Vi\u1EC7t-Anh tr\u00EAn n\u1EC1n v\u00ED d\u1EE5");
		translator = new Translator();

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 615, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] {
				350, 0, 89, 0
		};
		gbl_contentPane.rowHeights = new int[] {
				120, 23, 120, 265, 0
		};
		gbl_contentPane.columnWeights = new double[] {
				0.0, 1.0, 0.0, Double.MIN_VALUE
		};
		gbl_contentPane.rowWeights = new double[] {
				0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE
		};
		contentPane.setLayout(gbl_contentPane);

		buttonTranslate = new JButton("D\u1ECBch");
		buttonTranslate.addActionListener(this);

		JScrollPane scrollPaneInput = new JScrollPane();
		scrollPaneInput.setBorder(new TitledBorder(null, "C\u00E2u c\u1EA7n d\u1ECBch", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_scrollPaneInput = new GridBagConstraints();
		gbc_scrollPaneInput.fill = GridBagConstraints.BOTH;
		gbc_scrollPaneInput.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPaneInput.gridwidth = 3;
		gbc_scrollPaneInput.gridx = 0;
		gbc_scrollPaneInput.gridy = 0;
		contentPane.add(scrollPaneInput, gbc_scrollPaneInput);

		textAreaInput = new JTextArea();
		textAreaInput.setBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.GRAY));
		scrollPaneInput.setViewportView(textAreaInput);
		textAreaInput.setWrapStyleWord(true);
		textAreaInput.setLineWrap(true);

		JPanel panelMode = new JPanel();
		GridBagConstraints gbc_panelMode = new GridBagConstraints();
		gbc_panelMode.fill = GridBagConstraints.BOTH;
		gbc_panelMode.insets = new Insets(0, 0, 5, 5);
		gbc_panelMode.gridx = 0;
		gbc_panelMode.gridy = 1;
		contentPane.add(panelMode, gbc_panelMode);
		GridBagLayout gbl_panelMode = new GridBagLayout();
		gbl_panelMode.columnWidths = new int[] {
				110, 73, 59, 0
		};
		gbl_panelMode.rowHeights = new int[] {
				23, 0
		};
		gbl_panelMode.columnWeights = new double[] {
				0.0, 0.0, 0.0, Double.MIN_VALUE
		};
		gbl_panelMode.rowWeights = new double[] {
				0.0, Double.MIN_VALUE
		};
		panelMode.setLayout(gbl_panelMode);

		JLabel labelInputMode = new JLabel("Ch\u1EBF \u0111\u1ED9 t\u00E1ch t\u1EEB/g\u00E1n nh\u00E3n:");
		GridBagConstraints gbc_labelInputMode = new GridBagConstraints();
		gbc_labelInputMode.insets = new Insets(0, 0, 0, 5);
		gbc_labelInputMode.gridx = 0;
		gbc_labelInputMode.gridy = 0;
		panelMode.add(labelInputMode, gbc_labelInputMode);

		JRadioButton buttonAutomatic = new JRadioButton("T\u1EF1 \u0111\u1ED9ng");
		buttonAutomatic.setActionCommand("automatic");
		buttonAutomatic.setSelected(true);
		buttonGroupMode.add(buttonAutomatic);
		GridBagConstraints gbc_buttonAutomatic = new GridBagConstraints();
		gbc_buttonAutomatic.insets = new Insets(0, 0, 0, 5);
		gbc_buttonAutomatic.gridx = 1;
		gbc_buttonAutomatic.gridy = 0;
		panelMode.add(buttonAutomatic, gbc_buttonAutomatic);

		JRadioButton buttonManual = new JRadioButton("Th\u1EE7 c\u00F4ng");
		buttonManual.setActionCommand("manual");
		buttonGroupMode.add(buttonManual);
		GridBagConstraints gbc_buttonManual = new GridBagConstraints();
		gbc_buttonManual.anchor = GridBagConstraints.WEST;
		gbc_buttonManual.gridx = 2;
		gbc_buttonManual.gridy = 0;
		panelMode.add(buttonManual, gbc_buttonManual);
		GridBagConstraints gbc_buttonTranslate = new GridBagConstraints();
		gbc_buttonTranslate.anchor = GridBagConstraints.NORTH;
		gbc_buttonTranslate.fill = GridBagConstraints.HORIZONTAL;
		gbc_buttonTranslate.insets = new Insets(0, 0, 5, 0);
		gbc_buttonTranslate.gridx = 2;
		gbc_buttonTranslate.gridy = 1;
		contentPane.add(buttonTranslate, gbc_buttonTranslate);

		JScrollPane scrollPaneViSentence = new JScrollPane();
		scrollPaneViSentence.setBorder(new TitledBorder(null, "C\u00E2u v\u00ED d\u1EE5 ti\u1EBFng Vi\u1EC7t", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_scrollPaneViSentence = new GridBagConstraints();
		gbc_scrollPaneViSentence.fill = GridBagConstraints.BOTH;
		gbc_scrollPaneViSentence.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPaneViSentence.gridx = 0;
		gbc_scrollPaneViSentence.gridy = 2;
		contentPane.add(scrollPaneViSentence, gbc_scrollPaneViSentence);

		textAreaViSentence = new JTextArea();
		textAreaViSentence.setEditable(false);
		textAreaViSentence.setBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.GRAY));
		scrollPaneViSentence.setViewportView(textAreaViSentence);

		JScrollPane scrollPaneEnSentence = new JScrollPane();
		scrollPaneEnSentence.setBorder(new TitledBorder(null, "C\u00E2u ti\u1EBFng Anh t\u01B0\u01A1ng \u1EE9ng", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_scrollPaneEnSentence = new GridBagConstraints();
		gbc_scrollPaneEnSentence.fill = GridBagConstraints.BOTH;
		gbc_scrollPaneEnSentence.gridwidth = 2;
		gbc_scrollPaneEnSentence.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPaneEnSentence.gridx = 1;
		gbc_scrollPaneEnSentence.gridy = 2;
		contentPane.add(scrollPaneEnSentence, gbc_scrollPaneEnSentence);

		textAreaEnSentence = new JTextArea();
		textAreaEnSentence.setEditable(false);
		textAreaEnSentence.setBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.GRAY));
		scrollPaneEnSentence.setViewportView(textAreaEnSentence);

		JScrollPane scrollPaneOutput = new JScrollPane();
		scrollPaneOutput.setBorder(new TitledBorder(null, "K\u1EBFt qu\u1EA3 d\u1ECBch", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_scrollPaneOutput = new GridBagConstraints();
		gbc_scrollPaneOutput.fill = GridBagConstraints.BOTH;
		gbc_scrollPaneOutput.gridwidth = 3;
		gbc_scrollPaneOutput.gridx = 0;
		gbc_scrollPaneOutput.gridy = 3;
		contentPane.add(scrollPaneOutput, gbc_scrollPaneOutput);

		textAreaOuput = new JTextArea();
		textAreaOuput.setBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.GRAY));
		scrollPaneOutput.setViewportView(textAreaOuput);
		textAreaOuput.setEditable(false);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonTranslate) {
			do_bTranslate_actionPerformed(e);
		}
	}

	protected void do_bTranslate_actionPerformed(ActionEvent e) {
		String input = textAreaInput.getText();
		String output = null;

		String inputMode = buttonGroupMode.getSelection().getActionCommand();
		if (inputMode == "automatic") {
			output = translator.translate(input);
		}
		else if (inputMode == "manual") {
			output = translator.translate(WordUtils.parseWordArray(input));
		}

		textAreaViSentence.setText(translator.getVietnameseSentence());
		textAreaEnSentence.setText(translator.getEnglishSentence());

		if (output != null) {
			textAreaOuput.setText(output);
		}
		else {
			textAreaOuput.setText("No result.");
		}
	}
}
